/**
 * 
 */
package com.jm.web.data.form;

import java.util.Hashtable;
import java.util.Iterator;

import net.sf.json.JSONObject;

import com.jm.utils.StringBuilderEx;
import com.jm.utils.StringHelper;
import com.jm.utils.Utils;
import com.jm.web.data.BaseDataEntity;

/**
 * @author yanjie
 *
 * 2011/10/6 上午10:38:21
 */
public class Item {
    private String text ="";
    private String value ="";
    private boolean selected = false;
    protected Hashtable extAttrList = null;// new Hashtable();
    
    
    public Item() {
    }
    
    public void setProperty(String strName, String strValue)
	{
//    	strValue =  Utils.formatXML(strValue);
//    	strName =  Utils.formatXML(strName);
    	
    	if("text".equalsIgnoreCase(strName)){
			this.text = strValue;
		}
		
		if("value".equalsIgnoreCase(strName)){
			this.value = strValue;
		}
			
    	// 不明属性
		if (extAttrList == null)
			extAttrList = new Hashtable();
		extAttrList.put(strName.toLowerCase(), strValue);
	}
    
    public Item(String value,String text) {
//          this.text = Utils.formatXML(text);
//          this.value = Utils.formatXML(value.trim());
          this.text = text;
          this.value = value.trim();
    }

    public void setText(String text) {
        this.text = text;
    }

    public void setValue(String value) {
        this.value = value.trim();
    }

    public void setSelected(boolean selected) {
        this.selected = selected;
    }

    public String getText()
    {
        if(text == null)
            return "";
       return text;
    }

    public String getValue()
    {
        if(value == null)
            return "";
        return value;
    }

    public boolean getSelected() {
        return selected;
    }
    
    
    /**
	 * 判断是否存在指定键值
	 * @param strKey
	 * @return
	 */
	public boolean IsContainsKey(String strKey)
	{
		strKey = strKey.toLowerCase();
		if (extAttrList != null)
		{
			return extAttrList.containsKey(strKey);
		}
		return false;
	}

	/**
	 * @param strKey
	 * @param strDefault
	 * @return
	 */
	public String GetExtValue(String strKey, String strDefault)
	{
		strKey = strKey.toLowerCase();
		if (extAttrList != null)
			if (extAttrList.containsKey(strKey))
			{
				return (String) extAttrList.get(strKey);
			}
		return strDefault;
	}

	/**
	 * @param strKey
	 * @param nDefault
	 * @return
	 */
	public int GetExtValue(String strKey, int nDefault)
	{
		try
		{
			Integer tempValue = new Integer(nDefault);
			return Integer.parseInt(GetExtValue(strKey, tempValue.toString()));
		}
		catch (Exception ex)
		{
			return nDefault;
		}
	}

	/**
	 * @param strKey
	 * @param nDefault
	 * @return
	 */
	public long GetExtValue(String strKey, long nDefault)
	{
		try
		{
			Long nTempValue = new Long(nDefault);
			return Long.parseLong(GetExtValue(strKey, nTempValue.toString()));
		}
		catch (Exception ex)
		{
			return nDefault;
		}

	}

	/**
	 * @param strKey
	 * @param bDefault
	 * @return
	 */
	public boolean GetExtValue(String strKey, boolean bDefault)
	{
		try
		{
			return Boolean.parseBoolean(GetExtValue(strKey, bDefault ? "True" : "False"));
		}
		catch (Exception ex)
		{
			return bDefault;
		}
	}

	/**
	 * 设置附加属性
	 * 
	 * @param strKey
	 * @param strValue
	 */
	public void SetExtValue(String strKey, String strValue)
	{
		if("text".equalsIgnoreCase(strKey)){
			this.text = strValue;
		}
		
		if("value".equalsIgnoreCase(strKey)){
			this.value = strValue;
		}
			
    	// 不明属性
		if (extAttrList == null)
			extAttrList = new Hashtable();
		extAttrList.put(strKey.toLowerCase(), strValue);
	}

	/**
	 * 删除扩展属性
	 * @param strKey
	 */
	public void RemoveExtValue(String strKey)
	{
		if (extAttrList == null)
			return ;
		
		extAttrList.remove(strKey.toLowerCase());
	}
	
	
	/**
	 * 直接设置属性及值
	 * 
	 * @param strName
	 * @param strValue
	 */
	public void SetValue(String strName, String strValue)
	{
		strName = strName.toLowerCase();
		if (StringHelper.Length(strValue) != 0)
		{
			if (extAttrList == null)
				extAttrList = new Hashtable();
			extAttrList.put(strName, strValue);
		}
		else
		{
			if (extAttrList != null)
			{
				extAttrList.remove(strName);
			}
		}
	}
	
	public String getExtToString(){
		StringBuilderEx sb = new StringBuilderEx();
		if (extAttrList != null){
			for(Object object : extAttrList.keySet()){
				String key = StringHelper.getValue(object);
				sb.Append(" %1$s = \"%2$s\" ",key,StringHelper.getValue(extAttrList.get(key)));
			}
		}
		
		return sb.toString();
	}
	
	/**
	 * @param json
	 */
	public void FilledJson(JSONObject json){
		if(json == null) return ;
		
		json.put("text",this.getText());
		json.put("value",this.getValue());
		json.put("selected",this.getSelected());
		
		if(extAttrList != null)
			for(Iterator it= extAttrList.keySet().iterator();it.hasNext();){
				String key = (String)it.next();
				json.put(key,extAttrList.get(key));
			}
	}
	

	/**
	 * @param json
	 */
	public void FilledJson(StringBuilderEx jsonsb){
		if(jsonsb == null) return ;
		
		jsonsb.Append("\"text\":\"%1$s\"",this.getText());
		jsonsb.Append("\"value\":\"%1$s\"",this.getValue());
		jsonsb.Append("\"selected\":\"%1$s\"",this.getSelected());
		
		if(extAttrList != null)
			for(Iterator it= extAttrList.keySet().iterator();it.hasNext();){
				String key = (String)it.next();
				jsonsb.Append("\"%1$s\":\"%2$s\"",key,extAttrList.get(key));
			}
	}
	
	public void FilledBaseDataEntity(BaseDataEntity data){
		if(data == null) return ;
		
		data.spsv("text",this.getText());
		data.spsv("value",this.getValue());
		data.spbv("selected",this.getSelected());
		
		if(extAttrList != null)
			for(Iterator it= extAttrList.keySet().iterator();it.hasNext();){
				String key = (String)it.next();
				data.spov(key,extAttrList.get(key));
			}
	}
	
}
